package com.boatAnchorAssistant.service.controller.web;

import com.boatAnchorAssistant.pojo.param.user.*;
import com.boatAnchorAssistant.pojo.vo.user.*;
import com.boatAnchorAssistant.service.service.in.*;
import com.boatAnchorAssistant.service.utils.FileUtil;
import nirvana.core.context.WebContext;
import nirvana.core.domains.ann.Operation;
import nirvana.core.domains.ann.PreventBrush;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.domains.vo.ApiResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

import java.util.List;

/**
 *   web/用户
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 *
 */
@RestController
@RequestMapping("/user/web")
public class UserWebController {

    private final UserService service;

    public UserWebController(UserService service) {
        this.service = service;
    }


    /**
     * 创建用户
     * @param param
     * @return
     */
    @PostMapping("/createUser")
    public ApiResult createUser(@RequestBody @Valid CreateUserParam param) {
        service.createUser(param);
        return ApiResult.ok();
    }


    /**
     * 修改用户
     * @param param
     */
    @PutMapping("/editUser")
    public ApiResult editUser(@RequestBody @Valid EditUserParam param) {
        service.editUser(param);
        return ApiResult.ok();
    }

    /**
     * 移除用户
     * @param id
     */
    @DeleteMapping("/removeUser")
    public ApiResult removeUser(@RequestParam("id") Long id) {
        service.removeUser(id);
        return ApiResult.ok();
    }

    /**
     * 启动/禁用用户
     * @param id
     */
    @GetMapping("/enabledUser")
    public ApiResult enabledUser(@RequestParam("id") Long id) {
        service.enabledUser(id);
        return ApiResult.ok();
    }

    /**
     * 获取用户详情
     * @param id
     * @return
     */
    @GetMapping("/getUser")
    public ApiResult<GetUserVo> getUser(@RequestParam("id") Long id) {
        GetUserVo vo = service.getUser(id);
        return ApiResult.ok(vo);
    }

    /**
     * 获取用户列表
     * @param param
     * @return
     */
    @PostMapping("/getUserList")
    public ApiResult<PageResult<GetUserListVo>> getUserList(@RequestBody GetUserListParam param) {
        PageResult<GetUserListVo> result = service.getUserList(param);
        return ApiResult.ok(result);
    }


    /**
     * 获取当前用户信息
     *
     * @return
     */
    @GetMapping("/getCurrentUser")
    public ApiResult<GetCurrentUserVo> getCurrentUser() {
        GetCurrentUserVo vo = service.getCurrentUser();
        return ApiResult.ok(vo);
    }


    /**
     * 获取用户下拉列表
     *
     * @return
     */
    @PostMapping("/getUserDownList")
    public ApiResult<List<GetUserDownListVo>> getUserDownList(@RequestBody(required = false) @Valid GetUserDownListParam param) {
        List<GetUserDownListVo> list = service.getUserDownList(param);
        return ApiResult.ok(list);
    }


    /**
     * 根据角色获取用户
     *
     * @return
     */
    @PostMapping("/userListByRole")
    public ApiResult<PageResult<GetUserListByRoleVo>> getUserListByRole(@RequestBody @Valid GetUserListByRoleParam param) {
        PageResult<GetUserListByRoleVo> result = service.getUserListByRole(param);
        return ApiResult.ok(result);
    }


    /**
     * 用户绑定角色
     *
     * @return
     */
    @Operation(type = "编辑",value = "用户绑定角色")
    @PostMapping("/userBingRole")
    @PreventBrush(maxHits = 1,brushTime = 2000)
    public ApiResult userBingRole(@RequestBody @Valid UserBingRoleParam param) {
        service.userBingRole(param);
        return ApiResult.ok();
    }

    /**
     * 解除绑定角色
     *
     * @param param
     * @return
     */
    @Operation(type = "编辑",value = "解除绑定角色")
    @PutMapping("/userUnbindRole")
    public ApiResult userUnbindRole(@RequestBody @Valid UserBingRoleParam param) {
        service.userUnbindRole(param);
        return ApiResult.ok();
    }



    /**
     * 重置当前用户密码
     *
     * @return
     */
    @PostMapping("/resetCurrentUserPassword")
    public ApiResult resetCurrentUserPassword(@RequestBody @Valid ResetCurrentUserPasswordParam param) {
        service.resetCurrentUserPassword(param);
        return ApiResult.ok();
    }


    /**
     * 编辑用户密码
     *
     * @return
     */
    @PostMapping("/editUserPassword")
    public ApiResult editUserPassword(@RequestBody @Valid EditUserPasswordParam param) {
        service.editUserPassword(param);
        return ApiResult.ok();
    }

    /**
     * 导入图片
     * @param file
     * @return
     */
    @PostMapping("/uploadImg")
    public ApiResult uploadImg(@RequestParam("file") MultipartFile file) {
        String fileName= IdGenerator.UUID.generate();
        String add = FileUtil.uploadFile(fileName,file, WebContext.getCurrentUserId(),"head");
        return ApiResult.ok(add);
    }

}

